<template>
  <el-form ref="form" :rules="rules" :model="currentKnowledge" label-width="100px" class="demo-form">
    <el-row :gutter="10">
      <el-col :span="17">
        <el-card class="box-card">
          <el-form-item label="标题" prop="title">
            <el-input v-model="currentKnowledge.title"></el-input>
          </el-form-item>

          <el-form-item label="发布部门" prop="title">
            <el-input v-model="currentKnowledge.provider"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <editor v-model="currentKnowledge.content" height="300px"></editor>
          </el-form-item>
        </el-card>
      </el-col>
      <el-col :span="7">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>基本信息</span>
          </div>
          <el-form-item label="图片" prop="image">
            <image-uploader v-model="currentKnowledge.image" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
          </el-form-item>
          <el-form-item label="封面图" prop="image">
            <image-uploader v-model="currentKnowledge.cover" :max-size="10240" :width="uploadImgAttr.width" :height="uploadImgAttr.height"></image-uploader>
          </el-form-item>
          <el-form-item label="视频" prop="video">
            <media-uploader v-model="currentKnowledge.video"  accept="video" :max-size="51200" :width="90" :height="60"></media-uploader>
          </el-form-item>
        </el-card>
      </el-col>
    </el-row>

    <el-form-item>
      <el-button type="primary" v-if="currentKnowledge.status===3||currentKnowledge.status===2||currentKnowledge.status===1" @click="save('form',1)">保存</el-button>
      <el-button type="primary" v-if="currentKnowledge.status===3" @click="save('form',2)">提交审核</el-button>
      <el-button type="primary" v-if="currentKnowledge.status===3" v-has="{class:'knowledge:publish'}"
                 @click="save('form',3)">发布
      </el-button>

      <el-button @click="back">返回</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { mapGetters } from "vuex";
import Editor from "@/components/editor";
import ImageUploader from "@/components/image-uploader";
import MediaUploader from "@/components/media-uploader";

export default {
  props: {
    isClose: Boolean
  },
  created() {},
  mounted() {
    console.log(this.currentKnowledge);
  },
  computed: {
    ...mapGetters({
      currentKnowledge: "currentKnowledge",
      uploadImgAttr: "uploadImgAttr"
    })
  },
  data() {
    return {
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 3, max: 50, message: "长度在 3 到 50 个字符", trigger: "blur" }
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        childHomeId: [
          { required: true, message: "请选择儿童之家", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    save(formName, status) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.currentKnowledge.opt = status;
          this.$store
            .dispatch("addOrUpdateKnowledgeS", this.currentKnowledge)
            .then(ret => {
              this.$notify({
                title: "添加成功",
                type: "success"
              });
              this.back();
            });
        } else {
          return false;
        }
      });
    },
    back() {
      this.$emit("close");
    }
  },
  components: { Editor, ImageUploader, MediaUploader }
};
</script>
<style>
</style>
